/******************************************************************************
** 
**	File name: utest_50.c
**	
**	Description:	RTOS Ver.3 Unit Tests. Message Management from ISR.
**
**					This unit test is to check the message capabilities
**					when sending it from interrupts.
**
**  RTOS API Services to be checked:
**
**		- TBD
**
**  Notes:			This is MANUAL test!
**
*******************************************************************************
**
** Change history:
**	
** 09/25/2012 -AK- 	Introducing new unit test standard with RTOS resources
**					configuration moved to the dedicated source/header file
**
******************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>

#ifndef UTEST_50
#	error "UTEST_50 must be defined as -D$(Configuration) compiler option"
#endif

#include "UTEST_RES.h"

/******************************************************************************
	UNIT TEST INTERRUPT FUNCTION[s]
******************************************************************************/
/*
** Dummy Timer Handler
*/
RTOS_ISR_FUNC(TIMER_2_Handler)
{
	RTOS_DebugLog("[$2] entering interrupt handler");
	RTOS_DebugLogDump();
	RTOS_DebugPrint("[$2] sending message %4X to T3",&msg3);
	RTOS_SendMessageISR(&msq3,&msg3);	
	RTOS_DebugLog("[$2] leaving interrupt handler");
	RTOS_DebugLogDump();
}

/*
** User' Timer Handler
*/
RTOS_ISR_FUNC(TIMER_1_Handler)
{
	RTOS_DebugLog("[$1] entering interrupt handler");
	RTOS_DebugLogDump();
	RTOS_DebugPrint("[$1] sending message %4X to T1",&msg1);	
	RTOS_SendMessageISR(&msq1,&msg1);
	RTOS_DebugPrint("[$1] sending message %4X to T2",&msg2);	
	RTOS_SendMessageISR(&msq2,&msg2);
	RTOS_DebugLog("[$1] leaving interrupt handler");
	RTOS_DebugLogDump();
}

/******************************************************************************
	UNIT TEST TASK FUNCTIONs
******************************************************************************/
/*
** RTOS Task #0 - Stack checking task
*/
RTOS_TASK_FUNC(tsk0)
{
	RTOS_DebugPrint("[T0] started");
	while (1)
	{		
		RTOS_DebugLog("[T0] Check Stack");
	
		RTOS_StackCheck(tsk0);
		RTOS_StackCheck(tsk1);
		RTOS_StackCheck(tsk2);
		RTOS_StackCheck(tsk3);
		
		RTOS_DebugLog("[T0] suspending");
		
		RTOS_TaskSuspend(&tsk0PID);
		
		RTOS_DebugLog("[T0] resumed");
	}
}

/*
** RTOS Task #1
*/
RTOS_TASK_FUNC(tsk1)
{
	static RTOS_MSG_t	* msg;
	
	RTOS_DebugLog("[T1] started");
	
	UTST_FillRegisters();
	while(1)
	{
		RTOS_TaskResume(&tsk0PID);		// activate stack control check
		
		RTOS_DebugLog("[T1] blocks on wait");
		msg = RTOS_WaitMessage(&msq1);		
		RTOS_DebugPrint("[T1] message %4X received", msg);
	}
}

/*
** RTOS Task #2 
*/
RTOS_TASK_FUNC(tsk2)
{
	static RTOS_MSG_t	* msg;

	RTOS_DebugLog("[T2] started");
	
	UTST_FillRegisters();
	while(1)
	{
		RTOS_TaskResume(&tsk0PID);		// activate stack control check
		
		RTOS_DebugLog("[T2] blocks on wait");
		msg = RTOS_WaitMessage(&msq2);		
		RTOS_DebugPrint("[T2] message %4X received", msg);
	}
}

/*
** RTOS Task #3 
*/
RTOS_TASK_FUNC(tsk3)
{
	static RTOS_MSG_t	* msg;

	RTOS_DebugLog("[T3] started");

	UTST_FillRegisters();
	while(1)
	{
		RTOS_TaskResume(&tsk0PID);		// activate stack control check

		RTOS_DebugLog("[T3] blocks on wait");
		msg = RTOS_WaitMessage(&msq3);		
		RTOS_DebugPrint("[T3] message %4X received", msg);
	}
}

